﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.VendorsCommand;


namespace T.E.ERP.Write.API.Applicantions.CommandHandler.VendorsCommandHandler
{
    /// <summary>
    /// 原料提供商添加
    /// </summary>
    public class AddMateriCommandHandler : IRequestHandler<AddMateriCommand, APIResult<object>>
    {

        private readonly IBaseRepository<MaterialSupplier> matebaseRepository;

        /// <summary>
        /// 映射
        /// </summary>

        private readonly IMapper mapper;

        public AddMateriCommandHandler(IBaseRepository<MaterialSupplier> matebaseRepository, IMapper mapper)
        {
            this.matebaseRepository = matebaseRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddMateriCommand request, CancellationToken cancellationToken)
        {
           

            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();
            var entity = mapper.Map<MaterialSupplier>(request);
            await matebaseRepository.Add(entity);
            result.data = entity;
            return await Task.FromResult(result);
        }
    }
}
